<?php
/**
 * PHP Framework Integration HTTP Web Service class
 * This class represents a generic HTTP web service
 *
 * HTTP web services are capable to process HTTP requests which match
 * the following formats:
 * - filename.php?ClassName/MethodName&parameter1=XX&parameter2=XX
 * - filename.php?FunctionName&parameter1=XX&parameter2=XX
 * - filename.php?FunctionName&parameter1=XX&parameter2=XX&value1&value2
 *
 * Note that both named and unnamed parameters are supported.
 * Unnamed parameters are passed directly to the method / function
 * based on their order in the HTTP query string.
 * Named parameters are either passed by their order or
 * can be processed by the custom procedure.
 *
 * @author Andrew <andrew@vitche.com>
 * @author Frozen Rain <frozenrain@mail.ru>
 **/
abstract class HTTPWebService extends WebService {

    protected function processRequest() {

        // Locals
        $strClassName = null;
        $strMethodName = null;

        // Get class and method name
        $arRequest = $_REQUEST;
        foreach ($arRequest as $key => $value) {

            // Method name not parsed yet
            if (!isset($strMethodName)) {

                // Parse class and method name
                $arAtoms = split('/', $key);

                if (isset($arAtoms[1])) {
                    // Both class and method name
                    $strClassName = $arAtoms[0];
                    $strMethodName = $arAtoms[1];
                } else if(isset($arAtoms[0])) {
                    // Only method name
                    $strMethodName = $arAtoms[0];
                } else {
                    // No class and method name
                    // TODO: Investigate when occurs
                    throw new Exception('Unhandled exception at ' . __FILE__ . ':' . __LINE__);
                }

                unset($arRequest[$key]);
            }
            break;
        }

        if (isset($arRequest['PHPSESSID'])) {
            unset($arRequest['PHPSESSID']);
        }

        return $this->onMethodCall($strClassName, $strMethodName, $arRequest);
    }
}
?>